package com.helloandroid.paso.utils;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import android.net.Uri;
import android.sax.Element;
import android.sax.EndElementListener;
import android.sax.EndTextElementListener;
import android.sax.RootElement;
import android.sax.StartElementListener;
import android.util.Log;
import android.util.Xml;
import android.util.Xml.Encoding;

public class RssReader {

	/**
	 * Paso news rss structure:
	 * http://paso.hu/cutenews/rss.php?category=1&number=10
	 * 
	 * @author Tamas
	 * 
	 */
	public class RssConnector {

		static final String CHANNEL = "channel";
		static final String PUB_DATE = "pubDate";
		static final String DESCRIPTION = "description";
		static final String LINK = "link";
		static final String TITLE = "title";
		static final String ITEM = "item";
		static final String RSS = "rss";

		static final String CATEGORY = "category";
		static final String AUTHOR = "author";
		static final String COMMENTS = "comments";
		static final String LANGUAGE = "language";
		static final String ENCLOSURE = "enclosure";

		private RssItem mRssItem = new RssItem();
		private Encoding Encode;
		/**
		 * Default encoding without any parameter is ISO_8859_1
		 */
		public RssConnector() {
			Encode = Xml.Encoding.ISO_8859_1;
		}
		/**
		 * Changing default encoding by passing an Encoding object : Xml.Encoding.?
		 */
		public RssConnector(Encoding encoding) {
			Encode = encoding;
		}
		public List<RssItem> getRssItems(URL rssUrl) throws IOException, IOException, SAXException {
			final List<RssItem> articleList = new ArrayList<RssItem>();

			/* configure parsing */
			RootElement root = new RootElement(RSS);
			Element channel = root.getChild(CHANNEL);
			Element item = channel.getChild(ITEM);

			item.setStartElementListener(new StartElementListener() {

				@Override
				public void start(Attributes attributes) {
					mRssItem = new RssItem();
				}
			});
			item.setEndElementListener(new EndElementListener() {
				public void end() {
					articleList.add(mRssItem);
				}
			});

			item.getChild(TITLE).setEndTextElementListener(new EndTextElementListener() {
				public void end(String body) {
					mRssItem.mTitle = body;
				}
			});
			item.getChild(LINK).setEndTextElementListener(new EndTextElementListener() {
				public void end(String body) {
					mRssItem.mLink = Uri.parse(body).toString();
				}
			});

			item.getChild(COMMENTS).setEndTextElementListener(new EndTextElementListener() {
				public void end(String body) {
					try {
						mRssItem.mComments = new URL(body);
					} catch (MalformedURLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			});

			item.getChild(DESCRIPTION).setEndTextElementListener(new EndTextElementListener() {
				public void end(String body) {
					mRssItem.mDescription = body;
				}
			});

			item.getChild(PUB_DATE).setEndTextElementListener(new EndTextElementListener() {
				public void end(String body) {
					DateFormat df = DateFormat.getDateInstance(DateFormat.FULL, Locale.ENGLISH);
					try {
						mRssItem.mPublicationDate = df.parse(body);
					} catch (ParseException e) {
						df = DateFormat.getDateInstance(DateFormat.LONG, Locale.ENGLISH);
						try {
							mRssItem.mPublicationDate = df.parse(body);
						} catch (ParseException e1) {
							df = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.ENGLISH);
							try {
								mRssItem.mPublicationDate = df.parse(body);
							} catch (ParseException e2) {
								df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.ENGLISH);
								try {
									mRssItem.mPublicationDate = df.parse(body);
								} catch (ParseException e3) {
									Log.d("RssConnector", "input date format error");
								}
							}
						}
					}
				}
			});

			item.getChild(AUTHOR).setEndTextElementListener(new EndTextElementListener() {
				public void end(String body) {
					mRssItem.mAuthor = body;
				}
			});

			/* read the rss */
			InputStream rssInputStream = rssUrl.openConnection().getInputStream();

			/* trigger parsing */
			Xml.parse(rssInputStream, Encode, root.getContentHandler());

			mRssItem.mSyncTime = System.currentTimeMillis();
			return articleList;
		}
	}
}